#ifndef QPROPERTYLISTVIEW_H
#define QPROPERTYLISTVIEW_H

#include <QTreeWidget>

#include <QMap>
#include <QItemDelegate>
#include <QVector>
#include <QPair>
#include <QColor>

class QProperty;

class QObjectManager;
class QPropertyListView;
class QObjectProperty;
class AbstractCore;

class QPropertyEditorDelegate: public QItemDelegate
{
    Q_OBJECT
public :
    QPropertyEditorDelegate(QPropertyListView* parent):m_listView(parent){}
    QWidget* createEditor(QWidget *parent, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const;
    QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const;
    bool eventFilter(QObject *object, QEvent *event);
    void closeEditor(QProperty *property);

    QTreeWidgetItem     *editerItem();
protected slots:
    void slotEditorDestroyed(QObject *object);
protected:
    typedef QMap<QWidget*,QProperty*>   EditorToPropertyMap;
    EditorToPropertyMap     m_editorToProperty;

    QPropertyListView   *m_listView;

    mutable QList<QTreeWidgetItem*> m_editedItem;
    mutable QWidget*            m_editor;
};


class QPropertyListView: public QTreeWidget
{
    Q_OBJECT
public:
    QPropertyListView(AbstractCore *core,QWidget *parent=0);

    void    insertItem(QProperty* pro,QTreeWidgetItem *parent);

    QTreeWidgetItem*    addGroup(const QString &group);
    QTreeWidgetItem *groupItem(const QString &group);

    void            updateProperty(const QString &property);
    QObjectProperty* getObjectProperty(QObject* object);

    QObject         *object();
public slots:
    void    setObject(QObject *object);



private:
    friend class QPropertyEditorDelegate;
protected:
    void    updateValue();
    void    updateItem(QTreeWidgetItem* item);
    QProperty *itemToProperty(QTreeWidgetItem *item) const;
    QTreeWidgetItem *indexToItem(const QModelIndex &index)const;

    void drawRow(QPainter *painter, const QStyleOptionViewItem &options, const QModelIndex &index) const;

    void enableItem(QTreeWidgetItem *item)const;
    void disableItem(QTreeWidgetItem *item)const;

    void keyPressEvent(QKeyEvent *event);
    void mousePressEvent(QMouseEvent *event);

    QWidget *createEditor(QWidget* parent,QProperty *property);


protected slots:
    void valueChanged(QObjectProperty* pro,QProperty *property);
    void objectDestory();
signals:
    void changed();
    void nameChanged();
protected:

    QMap<QProperty*,QTreeWidgetItem*>   m_items;
    QList<QTreeWidgetItem*>             m_groupItems;
    QStringList                         m_groupList;

    QObjectManager  *m_manager;

    QVector<QPair<QColor,QColor> >   m_colors;

    QIcon                           m_expandIcon;
    QPropertyEditorDelegate         *m_delegate;

    QObject                         *m_object;
    QObjectProperty                 *m_objectProperty;
    AbstractCore                    *m_core;

};

#endif // QPROPERTYLISTVIEW_H
